﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using VocabManager.Code;
using System.Collections.Generic;
using Microsoft.Phone.Shell;
using System.Linq;

namespace VocabManager.View
{
    //base class for all views
    public class UserControlBase : UserControl, IDisposable
    {
        protected IApplicationBar ApplicationBar
        {
            get 
            {
                return ContainerPage.ApplicationBar;
            }
        }
        public UserControlBase()
        {
            this.Loaded += new RoutedEventHandler(UserControlBase_Loaded);
            this.Unloaded += new RoutedEventHandler(UserControlBase_Unloaded);
        }

        void UserControlBase_Unloaded(object sender, RoutedEventArgs e)
        {
            OnUnload();
        }
        void UserControlBase_Loaded(object sender, RoutedEventArgs e)
        {
             OnLoad();
        }


        protected virtual void OnLoad()
        {

        }


        protected virtual void OnUnload()
        {
            if (_appBarStateStoredFlage)
                ResotoreAppBarButtons();
        }
        ~UserControlBase()
        {
            Dispose();
        }

        public void Dispose()
        {

        }
        MainPage _containerPage;
        public MainPage ContainerPage
        {
            get
            {
             
                return App.Instance.MainPage;
            }
        }
        List<ApplicationBarIconButton> _oldButtons;
        bool _appBarStateStoredFlage;
        public void StoreAppBarButtons()
        {
            _appBarStateStoredFlage = true;
            _oldButtons = new List<ApplicationBarIconButton>();
            _oldButtons.AddRange(ContainerPage.ApplicationBar.Buttons.Cast <ApplicationBarIconButton>());
        }
        public void ResotoreAppBarButtons()
        {
            ContainerPage.ApplicationBar.Buttons.Clear();
            _oldButtons.ForEach(item => ContainerPage.ApplicationBar.Buttons.Add(item));
            
        }
    }
}
